# Values set at CMake configure time
set(CBDIR "@CMAKE_CURRENT_BINARY_DIR@")
set(CSDIR "@CMAKE_CURRENT_SOURCE_DIR@")
set(TNAME "@TNAME@")
set(LOGFILE "@LOG_FILE@")

set(BU_DIR_CACHE ${CBDIR}/cache_${TNAME})
set(LIBRT_CACHE ${CBDIR}/rtcache_${TNAME})
set(ENV{BU_DIR_CACHE} ${BU_DIR_CACHE})
set(ENV{LIBRT_CACHE} ${LIBRT_CACHE})
file(REMOVE_RECURSE "${BU_DIR_CACHE}")
file(REMOVE_RECURSE "${LIBRT_CACHE}")
file(MAKE_DIRECTORY "${BU_DIR_CACHE}")
file(MAKE_DIRECTORY "${LIBRT_CACHE}")

file(WRITE "${LOGFILE}" "Starting mged test run\n")

# The executable locations aren't know at CMake configure time, so it is passed
# in via the EXEC variable at runtime by a generator expression in the parent
# build.  De-quote it and assign it to the appropriate variable.
string(REPLACE "\\" "" MGED_EXEC "${EXEC}")
if(NOT EXISTS "${MGED_EXEC}")
  file(WRITE "${LOGFILE}" "mged not found at location \"${MGED_EXEC}\" - aborting\n")
  file(READ "${LOGFILE}" LOG)
  message(FATAL_ERROR "Unable to find mged, aborting.\nSee ${LOGFILE} for more details.\n${LOG}")
endif(NOT EXISTS "${MGED_EXEC}")

# Create the .g file
set(GFILE "${CBDIR}/${TNAME}.g")
execute_process(
  COMMAND "@CMAKE_COMMAND@" -E remove -f "${GFILE}"
)
execute_process(
  COMMAND "${MGED_EXEC}" -c "${GFILE}" ls
  RESULT_VARIABLE mged_result
  OUTPUT_VARIABLE mged_log
  ERROR_VARIABLE mged_log
)
file(APPEND "${LOGFILE}" "${mged_log}")
set(mged_log)
if(mged_result)
  file(READ "${LOGFILE}" LOG)
  message(FATAL_ERROR "[mged ${TNAME}] Failure: ${mged_result}. See ${LOGFILE} for more info.\n${LOG}")
endif(mged_result)

# Run the core test
execute_process(
  COMMAND "${MGED_EXEC}" -c "${GFILE}" < "${CBDIR}/regress_${TNAME}.mged"
  RESULT_VARIABLE mged_result
  OUTPUT_VARIABLE mged_log
  ERROR_VARIABLE mged_log
)
file(APPEND "${LOGFILE}" "${mged_log}")
set(mged_log)
if(mged_result)
  file(READ "${LOGFILE}" LOG)
  message(FATAL_ERROR "[mged ${TNAME}] Failure: ${mged_result}. See ${LOGFILE} for more info.\n${LOG}")
endif(mged_result)

# Clean up
execute_process(
  COMMAND "@CMAKE_COMMAND@" -E rm -rf ${BU_DIR_CACHE}
)
execute_process(
  COMMAND "@CMAKE_COMMAND@" -E rm -rf ${LIBRT_CACHE}
)
execute_process(
  COMMAND "@CMAKE_COMMAND@" -E remove -f "${GFILE}"
)

# Local Variables:
# tab-width: 8
# mode: cmake
# indent-tabs-mode: t
# End:
# ex: shiftwidth=2 tabstop=8
